namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerDefine : Page
    {
        protected DropDownList AreaDdl;
        protected DataList CustomerDefDL;
        protected Image iCustomerDefManage;
        protected Label lbArea;
        protected UserPrincipal Principal = null;
        protected DataList SchoolDefDL;
        protected DataList TrainDefDL;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CustomerDefDLDataBind();
        }

        private void CustomerDefDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.CustomerDefDL.EditItemIndex = -1;
            this.CustomerDefDLDataBind();
        }

        private void CustomerDefDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            if (!ManageCustomerDef.IsExistDefInOtherTable((Guid) this.CustomerDefDL.DataKeys[e.Item.ItemIndex]))
            {
                ManageCustomerDef.RemoveDefViaId((Guid) this.CustomerDefDL.DataKeys[e.Item.ItemIndex]);
                this.CustomerDefDL.EditItemIndex = -1;
                this.CustomerDefDLDataBind();
            }
            else
            {
                base.Response.Write("<script> alert('还有相关信息未被删除！') </script>");
            }
        }

        private void CustomerDefDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.CustomerDefDL.EditItemIndex = e.Item.ItemIndex;
            this.CustomerDefDLDataBind();
        }

        private void CustomerDefDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewName");
                TextBox NameEnTbx = (TextBox) e.Item.FindControl("txtNewNameEn");
                if ((NameTbx != null) && (NameEnTbx != null))
                {
                    string DefName = NameTbx.Text.Trim();
                    string DefNameEn = NameEnTbx.Text.Trim();
                    if ((DefName != "") && (DefNameEn != ""))
                    {
                        if (ManageCustomerDef.IsExistSameDefName(DefName, new Guid(this.AreaDdl.SelectedValue)))
                        {
                            base.Response.Write("<script> alert('已存在定义') </script>");
                            this.CustomerDefDLDataBind();
                        }
                        else
                        {
                            ManageCustomerDef.InsertNewDef(DefName, DefNameEn, new Guid(this.AreaDdl.SelectedValue));
                            this.CustomerDefDLDataBind();
                        }
                    }
                }
            }
        }

        private void CustomerDefDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
            TextBox txtNameEn = e.Item.FindControl("txtNameEn") as TextBox;
            if ((txtName != null) && (txtNameEn != null))
            {
                string DefName = txtName.Text.Trim();
                string DefNameEn = txtNameEn.Text.Trim();
                if (!(DefName == "") && !(DefNameEn == ""))
                {
                    ManageCustomerDef.UpdateDef(DefName, DefNameEn, (Guid) this.CustomerDefDL.DataKeys[e.Item.ItemIndex]);
                    this.CustomerDefDL.EditItemIndex = -1;
                    this.CustomerDefDLDataBind();
                }
            }
        }

        private void CustomerDefDLDataBind()
        {
            this.CustomerDefDL.DataSource = ManageCustomerDef.GetCustomerDef(new Guid(this.AreaDdl.SelectedValue));
            this.CustomerDefDL.DataBind();
        }

        private void CustomerSchoolDLDataBind()
        {
            this.SchoolDefDL.DataSource = ManageCustomerSchool.GetCustomerSchool();
            this.SchoolDefDL.DataBind();
        }

        private void CustomerTrainDLDataBind()
        {
            this.TrainDefDL.DataSource = ManageCustomerTrain.GetCustomerTrain();
            this.TrainDefDL.DataBind();
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("CustomerDefine", str, language, path);
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.CustomerDefDL.ItemCommand += new DataListCommandEventHandler(this.CustomerDefDL_ItemCommand);
            this.CustomerDefDL.CancelCommand += new DataListCommandEventHandler(this.CustomerDefDL_CancelCommand);
            this.CustomerDefDL.EditCommand += new DataListCommandEventHandler(this.CustomerDefDL_EditCommand);
            this.CustomerDefDL.UpdateCommand += new DataListCommandEventHandler(this.CustomerDefDL_UpdateCommand);
            this.CustomerDefDL.DeleteCommand += new DataListCommandEventHandler(this.CustomerDefDL_DeleteCommand);
            this.TrainDefDL.ItemCommand += new DataListCommandEventHandler(this.TrainDefDL_ItemCommand);
            this.TrainDefDL.CancelCommand += new DataListCommandEventHandler(this.TrainDefDL_CancelCommand);
            this.TrainDefDL.EditCommand += new DataListCommandEventHandler(this.TrainDefDL_EditCommand);
            this.TrainDefDL.UpdateCommand += new DataListCommandEventHandler(this.TrainDefDL_UpdateCommand);
            this.TrainDefDL.DeleteCommand += new DataListCommandEventHandler(this.TrainDefDL_DeleteCommand);
            this.SchoolDefDL.ItemCommand += new DataListCommandEventHandler(this.SchoolDefDL_ItemCommand);
            this.SchoolDefDL.CancelCommand += new DataListCommandEventHandler(this.SchoolDefDL_CancelCommand);
            this.SchoolDefDL.EditCommand += new DataListCommandEventHandler(this.SchoolDefDL_EditCommand);
            this.SchoolDefDL.UpdateCommand += new DataListCommandEventHandler(this.SchoolDefDL_UpdateCommand);
            this.SchoolDefDL.DeleteCommand += new DataListCommandEventHandler(this.SchoolDefDL_DeleteCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
                this.CustomerDefDLDataBind();
                this.CustomerTrainDLDataBind();
                this.CustomerSchoolDLDataBind();
            }
        }

        private void SchoolDefDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.SchoolDefDL.EditItemIndex = -1;
            this.CustomerSchoolDLDataBind();
        }

        private void SchoolDefDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            ManageCustomerSchool.RemoveSchoolViaId((Guid) this.SchoolDefDL.DataKeys[e.Item.ItemIndex]);
            this.SchoolDefDL.EditItemIndex = -1;
            this.CustomerSchoolDLDataBind();
        }

        private void SchoolDefDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.SchoolDefDL.EditItemIndex = e.Item.ItemIndex;
            this.CustomerSchoolDLDataBind();
        }

        private void SchoolDefDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewSchoolName");
                TextBox NameEnTbx = (TextBox) e.Item.FindControl("txtNewSchoolEnName");
                if ((NameTbx != null) && (NameEnTbx != null))
                {
                    string DefName = NameTbx.Text.Trim();
                    string DefNameEn = NameEnTbx.Text.Trim();
                    if ((DefName != "") && (DefNameEn != ""))
                    {
                        if (ManageCustomerSchool.IsExistSameSchoolName(DefName))
                        {
                            base.Response.Write("<script> alert('已存在定义') </script>");
                            this.CustomerDefDLDataBind();
                        }
                        else
                        {
                            ManageCustomerSchool.InsertNewSchool(DefName, DefNameEn);
                            this.CustomerSchoolDLDataBind();
                        }
                    }
                }
            }
        }

        private void SchoolDefDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtSchoolName") as TextBox;
            TextBox txtNameEn = e.Item.FindControl("txtSchoolEnName") as TextBox;
            if ((txtName != null) && (txtNameEn != null))
            {
                string DefName = txtName.Text.Trim();
                string DefNameEn = txtNameEn.Text.Trim();
                if (!(DefName == "") && !(DefNameEn == ""))
                {
                    ManageCustomerSchool.UpdateSchool(DefName, DefNameEn, (Guid) this.SchoolDefDL.DataKeys[e.Item.ItemIndex]);
                    this.SchoolDefDL.EditItemIndex = -1;
                    this.CustomerSchoolDLDataBind();
                }
            }
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerDefManage.ImageUrl = "images/titile_CustomerDefMgt.jpg";
                    break;

                case "en":
                    this.iCustomerDefManage.ImageUrl = "images/titile_CustomerDefMgtEn.jpg";
                    break;
            }
        }

        private void TrainDefDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.TrainDefDL.EditItemIndex = -1;
            this.CustomerTrainDLDataBind();
        }

        private void TrainDefDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            ManageCustomerTrain.RemoveTrainViaId((Guid) this.TrainDefDL.DataKeys[e.Item.ItemIndex]);
            this.TrainDefDL.EditItemIndex = -1;
            this.CustomerTrainDLDataBind();
        }

        private void TrainDefDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.TrainDefDL.EditItemIndex = e.Item.ItemIndex;
            this.CustomerTrainDLDataBind();
        }

        private void TrainDefDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewTrainName");
                TextBox NameEnTbx = (TextBox) e.Item.FindControl("txtNewTrainEnName");
                if ((NameTbx != null) && (NameEnTbx != null))
                {
                    string DefName = NameTbx.Text.Trim();
                    string DefNameEn = NameEnTbx.Text.Trim();
                    if ((DefName != "") && (DefNameEn != ""))
                    {
                        if (ManageCustomerTrain.IsExistSameTrainName(DefName))
                        {
                            base.Response.Write("<script> alert('已存在定义') </script>");
                            this.CustomerDefDLDataBind();
                        }
                        else
                        {
                            ManageCustomerTrain.InsertNewTrain(DefName, DefNameEn);
                            this.CustomerTrainDLDataBind();
                        }
                    }
                }
            }
        }

        private void TrainDefDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtTrainName") as TextBox;
            TextBox txtNameEn = e.Item.FindControl("txtTrainEnName") as TextBox;
            if ((txtName != null) && (txtNameEn != null))
            {
                string DefName = txtName.Text.Trim();
                string DefNameEn = txtNameEn.Text.Trim();
                if (!(DefName == "") && !(DefNameEn == ""))
                {
                    ManageCustomerTrain.UpdateTrain(DefName, DefNameEn, (Guid) this.TrainDefDL.DataKeys[e.Item.ItemIndex]);
                    this.TrainDefDL.EditItemIndex = -1;
                    this.CustomerTrainDLDataBind();
                }
            }
        }
    }
}

